package com.kai.module_cartoon

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.snackbar.Snackbar
import com.hwangjr.rxbus.RxBus
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.thread.EventThread
import com.kai.base.fragment.BaseMvFragment
import com.kai.base.utils.LogUtils
import com.kai.lib_app.core.FRAGMENT_ID_CARTOON
import com.kai.lib_app.core.RouterHub
import com.kai.lib_app.data.event.EndHomeAnimEvent
import com.kai.lib_app.data.event.UpdateMainEvent
import com.kai.lib_app.helper.RouteHelper
import com.kai.lib_app.helper.launchReadingListActivity
import com.kai.module_cartoon.cart.CartoonBanner
import com.kai.module_cartoon.cart.CartoonBean
import com.kai.module_cartoon.cart.CartoonViewMode
import com.kai.module_cartoon.databinding.CartoonFramentBinding
import com.scwang.smart.refresh.header.MaterialHeader
import com.youth.banner.config.BannerConfig
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.util.BannerUtils


/**
 * @Author: 小强同学
 * @Date: 2021/3/18 15:52
 * @Description:  漫画
 */
@Route(path = RouterHub.ROUTER_CARTOON_FRAGMENT)
class CartoonFrament : BaseMvFragment<CartoonViewMode, CartoonFramentBinding>() {

    override fun getBinding(inflater: LayoutInflater, viewGroup: ViewGroup?): CartoonFramentBinding = CartoonFramentBinding.inflate(inflater, viewGroup, false)

    override fun isRegisterRxBus(): Boolean = true

    lateinit var adaper: CartoonBanner

    override fun initViews() {
        super.initViews()
        initRefresh()
        initHead()
    }

    private fun initRefresh() {
        mRootView?.apply {
            mSmartRefreshLayout.setRefreshHeader(MaterialHeader(context))
            mSmartRefreshLayout.setOnRefreshListener {
                initBanner()
                initSelect()
                initHot()
                initUpdate()
                initLately()
            }
            mSmartRefreshLayout?.autoRefresh()
        }
    }

    private fun initBanner() {
        mViewModel?.loadBannerData()?.observe(this, Observer {
            mRootView?.mSmartRefreshLayout?.finishRefresh(true)
            setBannerData(it)
            RxBus.get().post(EndHomeAnimEvent(FRAGMENT_ID_CARTOON))
        })
    }

    private fun initHead() {
//        mViewModel?.loadHeadData()?.observe(this, Observer {
//
//        })
    }

    private fun initSelect() {
        mViewModel?.loadSelected()?.observe(this, Observer {
            mRootView?.hlvSelected?.setUpdate(it)
        })
    }

    private fun initHot() {
        mViewModel?.loadHot()?.observe(this, Observer {
            mRootView?.hlvHot?.setUpdate(it)
        })
    }

    private fun initUpdate() {
        mViewModel?.loadUpdate()?.observe(this, Observer {
            mRootView?.hlvUpdate?.setUpdate(it)
        })
    }

    private fun initLately() {
        mViewModel?.loadLately()?.observe(this, Observer {
            mRootView?.hlvLately?.setUpdate(it)
        })
    }

    private fun setBannerData(list: MutableList<CartoonBean.BannerData>) {
        adaper = CartoonBanner(list)
        mRootView?.apply {
            mBanner.setAdapter(adaper)
                .setIndicator(CircleIndicator(context)) //设置指示器
                .setIndicatorSelectedColorRes(R.color.style_color) //设置指示器--选中颜色
                .setIndicatorNormalColorRes(R.color.white)//设置指示器--未选中颜色
                .setIndicatorGravity(IndicatorConfig.Direction.RIGHT)//设置指示器--在右边
                .setIndicatorMargins(
                    IndicatorConfig.Margins(
                        0, 0,
                        BannerConfig.INDICATOR_MARGIN,
                        BannerUtils.dp2px(12f).toInt()
                    )
                )// 设置边界
                .addBannerLifecycleObserver(this@CartoonFrament)//添加生命周期观察者
                .setOnBannerListener { data, _ ->
                  val bannerData=  data as CartoonBean.BannerData
                    launchReadingListActivity(bannerData.urlIndex,bannerData.imageMsg)
                }
        }

    }

    @Subscribe(thread = EventThread.MAIN_THREAD)
    fun onMessageEvent(messageEvent: UpdateMainEvent) {
        if (messageEvent.index == FRAGMENT_ID_CARTOON) {
            mRootView?.mSmartRefreshLayout?.autoRefresh()
        }
    }


}